package com.opal.modules.costReportForm.mapper.activiti;

import com.opal.consts.WSC;
import com.opal.domain.R;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.*;
import org.activiti.engine.history.HistoricProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Repository
@Slf4j
public class ActivitiMapper {
    @Autowired
    private ProcessEngine processEngine;

    /**
     * 查询：根据业务id查询当前流程实例对象(所有的,也就是包括运行中的)
     *
     * @param business
     * @return
     */
    public List<HistoricProcessInstance> queryHistoryProcessByBusiness(String business) {
        HistoryService historyService = processEngine.getHistoryService();
        //根据业务id，查询实例
        List<HistoricProcessInstance> lists = historyService.createHistoricProcessInstanceQuery()
                .processInstanceBusinessKey(business)   //条件一：成本报告单据号
                .list();

        return lists;
    }

    /**
     * 删除：结束流程实例
     *
     *  deleteProcessInstance方法：参数1：流程实例id，参数2：删除原因描述
     *   删除(结束)流程，不管有任务是否完成：注意但是历史表是不会删除的，有"删除原因会记录在act_hi_actinst表中
     * @param processInstanceId 流程实例id
     * @param description        结束流程的原因描述
     * @return
     */
    public R deleteProcessInstance(@PathVariable("processInstanceId") String processInstanceId, String description) {
        //删除（结束）流程，不管有任务是否完成：注意但是历史表是不会删除的，有"删除原因会记录在act_hi_actinst表中
        try {
            RuntimeService runtimeService = processEngine.getRuntimeService();
            runtimeService.deleteProcessInstance(processInstanceId, description);
        }catch (Exception e){
            e.printStackTrace();
            return  new R(WSC.REQUEST_ERROR, "结束流程失败！");
        }

        return new R(WSC.RESTPONSE_OK, "结束流程成功！");
    }

    /**
     * 删除：所有的流程实例信息都会进行删除
     *
     *注意：1、若流程实例还在运行中，会抛出异常ActivitiException: Process instance is still running, cannot delete historic process instance: 95147  (流程实例id)，内部已实现了事务
     * 2、若没有此流程实例会抛出异常，ActivitiObjectNotFoundException
     * @param processInstanceId 流程实例id
     * @return
     */
    public R deleteHistoryProcessInstance(@PathVariable("processInstanceId") String processInstanceId) {
        try {
            HistoryService historyService = processEngine.getHistoryService();
            historyService.deleteHistoricProcessInstance(processInstanceId);
        }catch (ActivitiObjectNotFoundException e){  //此异常是没有找到：不能抛出异常，
            String str="系统中没有相关的数据，删除失败！具体内容："+e.getMessage();
            log.info(str);
            return new R(WSC.REQUEST_ERROR, str);
        }catch (ActivitiException e){//删除一个流程未结束的异常
            e.printStackTrace();
            return new R(WSC.REQUEST_ERROR, "删除失败：不能删除未结束的流程");
        } catch (Exception e) {
            e.printStackTrace();
            return new R(WSC.REQUEST_ERROR, "系统维护中，请稍后尝试再进行删除操作！");
        }

        return new R(WSC.RESTPONSE_OK, "删除成功！");
    }


}
